<?php
class Plan_Model_PlanUsageDetail
{
    protected $_dbTable;
	
	public function __construct(){
		$this->_dbTable = $this->getDbTable();
	}
    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('非正确的table类型！');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Plan_Model_DbTable_PlanUsageDetail');
        }
        return $this->_dbTable;
    }
	
	
	public function add($row){
//		$table = $this->getDbTable();
		return $this->getDbTable()->insert($row);
	}
	
	//列表
    public function getByCondition($condition=array(),$type='*',$orderby="pud_id",$pageSize=0,$page=0){
		$select = $this->getDbTable()->select()->from($this->_dbTable,$type);
        $select->where("1 =?",1);
		if(isset($condition["customer_id"]) && $condition["customer_id"]!=''){
            $select->where("customer_id =?",$condition["customer_id"]);
        }
		if(isset($condition["pu_id"]) && $condition["pu_id"]!=''){
            $select->where("pu_id =?",$condition["pu_id"]);
        }
		if(isset($condition["app_type"]) && $condition["app_type"]!=''){
            $select->where("app_type =?",$condition["app_type"]);
        }
		if(isset($condition["type"]) && $condition["type"]!=''){
            $select->where("type =?",$condition["type"]);
        }
        if('count(*)'==$type){
            return $this->_dbTable->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_dbTable->getAdapter()->fetchAll($sql);
        }
	}

}